import configparser
from sendapi.BaseSend import *
class Account():
    def __init__(self):
        self.Version='1.0.1'
        self.inipath='./program/data/account/data.ini'
        # self.inipath='./data/account/data.ini'
        self.open=True
        self.config = configparser.ConfigParser()
        self.config.read(self.inipath,'utf-8')
        self.section=self.config.get('init','section')
        self.keyWord=['版本-记账助手','帮助-记账助手','记账','清单','退账','清账','更换账本','新建账本','账本','收入']
    def help(self):
            str='*'*10
            str+='\n'
            for i in self.keyWord:
                str+=(i+'\n')
            str+='*'*10
            FriendApi(2384945985,1656301062).sendWord(str)
    def addAccount(self,name:str):
            self.config.add_section(name)
            self.config.write(open(self.inipath, "w",encoding='utf-8'))
            FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n添加新帐本:"+name)
    def delAccount(self,name:str):
            status=self.config.remove_section(name)
            if status:
                FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n删除成功")
            else:
                FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n没有该清单")
            self.config.write(open(self.inipath, "w",encoding='utf-8'))
    def changeAccount(self,section):
            self.config.set('init','section',section)
            self.section=section
            FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n账本切换到:"+self.section)
            self.config.write(open(self.inipath, "w"))
    def addItem(self,option,value,cmd):
        if cmd==0:
            self.config.set(self.section,option,value)
            FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n添加:"+option+"\n支出:"+value)
            self.config.write(open(self.inipath, "w",encoding='utf-8'))
        elif cmd==1:
            self.config.set(self.section,option,'-'+value)
            FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n添加:"+option+"\n收入:"+value)
            self.config.write(open(self.inipath, "w",encoding='utf-8'))
    def delItem(self,option):
            status=self.config.remove_option(self.section,option)
            if status:
                FriendApi(2384945985,1656301062).sendWord('记账助手：\n----------\n'+option+"删除成功")
            else:
                FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n未找到"+option)
            self.config.write(open(self.inipath, "w",encoding='utf-8'))
    def changeItem(self,option,value):
            self.addItem(option=option,value=value,cmd=0)
            FriendApi(2384945985,1656301062).sendWord("记账助手：\n----------\n信息修改成功"+option)
    def showAccount(self):
            sectionList=self.config.sections()[1:]
            str='=====账本=====\n'
            for i in sectionList:
                str+=(i+'\n')
            str+='============='
            FriendApi(2384945985,1656301062).sendWord(str)
    def showItem(self):
            optionList=self.config.items(self.section)
            strs='====='+self.section+'=====\n'
            length=len(strs)
            sum=0
            for i in optionList:
                strs+=(i[0]+' : '+i[1]+'\n')
                try:
                    num=int(i[1])
                except:
                    num=0
                sum+=num
            strs+=('-'*length)
            strs+=('\n总计花费：'+str(sum)+'\n')
            strs+=('='*length)
            FriendApi(2384945985,1656301062).sendWord(strs)
    def run(self):
        self.open=True
        FriendApi(2384945985,1656301062).sendWord('记账助手：已开启')
    def stop(self):
        self.open=False
        FriendApi(2384945985,1656301062).sendWord('记账助手：已关闭')
    def version(self):
        FriendApi(2384945985,1656301062).sendWord('版本 : 记账助手-'+self.Version)
